home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / T / The Journal / cdev / Journal cdev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-16  |  11.0 KB  |  497 lines  |  [TEXT/KAHL]

  1. /*
  2.     FILE: Journal cdev.c - based on Journal cdev.c
  3.      
  4.      Parts of this code Copyright (c) 1989 Symantec Corporation.  All rights reserved.
  5.  
  6.  
  7.      This is the cdev part of The Journal. The cdev allows configuration of The
  8.      Journal, it also invokes the Journal driver.
  9.      
  10.      The cdev allows specification of the types of events journaled, the file to
  11.      journal to (or a default), the level of time stamping.
  12.      
  13.  */
  14.  
  15. #include "Journal cdev.h"
  16. #include "journalDriverInclude.h"
  17.  
  18. /*----------------------
  19.  
  20.     Runnable
  21.     
  22.     should the cdev appear in the Control Panel?
  23.  
  24.     This implements the "macDev" message.
  25.  
  26. ------------------------*/
  27.  
  28. Boolean Runnable(void)
  29. {
  30.     return(true);
  31. }
  32.  
  33.  
  34. /*----------------------
  35.  
  36.     New
  37.     
  38.     create the Journal cdev object
  39.  
  40. ------------------------*/
  41.  
  42. cdev *New(void)
  43. {
  44.     return(new(Journal));
  45. }
  46.  
  47.  
  48.  
  49. /*----------------------
  50.  
  51.     Init
  52.  
  53.     initialize the cdev. This involves seting up the control
  54.     items according to the saved state of the cdev.
  55.     
  56.     The check boxes for different types of events are checked
  57.     or unchecked according to the previous state. The button
  58.     to set the journal file is enabled if no file is picked,
  59.     disabled otherwise. The start/stop button is enabled and
  60.     labled start if a file is picked, but the journal is not
  61.     recording. It is enabled and labeled stop if the journal
  62.     is recording. It is disabled if no file is currently
  63.     picked
  64.     
  65. ------------------------*/
  66.  
  67. void Journal::Init(void)
  68. {
  69.     P_JrnlState        theJrnlState ;
  70.     ControlHandle    theControl ;
  71.     int                aType, i ;
  72.     Rect            aRect ;
  73.     char            *osPtr, creatStr[5] ;
  74.     
  75.     
  76.     int     **theFlag ;
  77.     Handle    itemHandle ;
  78.  
  79. /* some feedback about the driver. This will evetually go away */
  80.     
  81.     GetDItem(dp, lastItem + INSTALLED_STRING, &aType, &itemHandle, &aRect) ;
  82.     theFlag = (int**) GetResource(JRNL_DRIVER_FLAG_TYPE, JRNL_DRIVER_FLAG_ID) ;
  83.     if ((**theFlag) == 0)
  84.         SetIText(itemHandle, "\pDriver NOT Installed") ;
  85.     else
  86.         SetIText(itemHandle, "\pDriver Installed") ;
  87.  
  88.     /* get the journal state resource and set things up */
  89.         
  90.     journalState = GetResource(STATE_RES_TYPE, STATE_RES_ID) ;
  91.     if (journalState == NULL) {
  92.         /* error condition */
  93.     }
  94.     else {
  95.         HLock(journalState) ;
  96.         theJrnlState = (P_JrnlState)(*journalState) ;
  97.         
  98.         /* first set the state of the check boxe */
  99.         
  100.         DoSetControl(lastItem + TIME_STAMP_CHECK,
  101.                         (theJrnlState->eventFlags & TIME_STAMP_BIT)) ;
  102.  
  103.         /* set the active state of all buttons (except start/stop) 
  104.            to deactive if the journal is recording, active otherwise.
  105.            
  106.            Also set the string of the start/stop button. */
  107.         
  108.         if (theJrnlState->recording != 0) {
  109.             GetDItem(dp, lastItem + START_STOP_BUTTON, &aType,
  110.                      (Handle)&theControl, &aRect) ;
  111.             SetCTitle(theControl, STOP_STRING) ;
  112.             SetButtonState(FALSE) ;
  113.         }
  114.         else {
  115.             GetDItem(dp, lastItem + START_STOP_BUTTON, &aType,
  116.                      (Handle)&theControl, &aRect) ;
  117.             SetCTitle(theControl, START_STRING) ;
  118.             SetButtonState(TRUE) ;
  119.         }
  120.         
  121.         /* output the string for the current journal file creator type */
  122.  
  123.         osPtr = (char*) &(theJrnlState->creator) ;
  124.         creatStr[0] = 4 ;
  125.         creatStr[1] = *osPtr++ ; creatStr[2] = *osPtr++ ;
  126.         creatStr[3] = *osPtr++ ; creatStr[4] = *osPtr++ ;
  127.         
  128.         GetDItem(dp, lastItem + FILE_CREATOR_STRING, &aType, &itemHandle, &aRect) ;
  129.         SetIText(itemHandle, creatStr) ;
  130.  
  131.         
  132.         
  133.         /* finaly set the state of the start/stop button based on
  134.            whether there is a currently chosen journal file */
  135.         
  136.         GetDItem(dp, lastItem + START_STOP_BUTTON, &aType, (Handle)&theControl, &aRect) ;
  137.         if (theJrnlState->hasFile != 0)
  138.             HiliteControl(theControl, CONTROL_ACTIVE) ;
  139.         else
  140.             HiliteControl(theControl, CONTROL_INACTIVE) ;
  141.         
  142.         HUnlock(journalState) ;
  143.     }
  144.     
  145.     inherited::Init();
  146. }
  147.  
  148.  
  149. /*----------------------
  150.  
  151.     Close
  152.     
  153.     Close needs to deallocate the journal state resource.
  154.     It writes out the resource just in case it has changed.
  155.  
  156. ------------------------*/
  157.  
  158. void Journal::Close(void)
  159. {
  160.     ChangedResource(journalState) ;
  161.     WriteResource(journalState) ;
  162.     ReleaseResource(journalState) ;
  163.     inherited::Close();
  164. }
  165.  
  166.  
  167. /*----------------------
  168.  
  169.     ItemHit
  170.     
  171.     An active item has been hit in the cdev. Find out which
  172.     one it was and take the appropriate action.
  173.  
  174. ------------------------*/
  175.  
  176. void Journal::ItemHit(int item)
  177. {
  178.     ControlHandle    theControl ;
  179.     int                aType, controlValue ;
  180.     Rect            aRect ;
  181.     P_JrnlState        theJrnlState ;
  182.  
  183.     switch (item) {
  184.         case FILE_BUTTON:
  185.             DoFileButton() ;
  186.             break ;
  187.             
  188.         case TIME_STAMP_CHECK:
  189.             GetDItem(dp, lastItem + item, &aType, (Handle)&theControl, &aRect) ;
  190.             controlValue = (GetCtlValue(theControl) == CHECK_ON) ? CHECK_OFF : CHECK_ON ;
  191.             SetCtlValue(theControl, controlValue) ;
  192.             
  193.             HLock(journalState) ;
  194.             theJrnlState = (P_JrnlState)(*journalState) ;
  195.             switch (item) {            
  196.                 case TIME_STAMP_CHECK:
  197.                     if (controlValue == CHECK_ON)
  198.                         theJrnlState->eventFlags |= TIME_STAMP_BIT ;
  199.                     else
  200.                         theJrnlState->eventFlags &= ~(TIME_STAMP_BIT) ;
  201.                     break ;
  202.             }    
  203.             HUnlock(journalState) ;
  204.             ChangedResource(journalState) ;
  205.             WriteResource(journalState) ;    
  206.             break ;
  207.             
  208.         case START_STOP_BUTTON:
  209.             DoStartStopButton() ;
  210.             break ;
  211.             
  212.         case FILE_CREATOR_BUTTON:
  213.             DoSetFileCreator() ;
  214.             break ;
  215.     }
  216.  
  217. }
  218.  
  219.  
  220. /*----------------------
  221.  
  222.     DoFileButton
  223.  
  224.     The user pressed the file button to select a
  225.     journal file.
  226.     
  227.     Use the standard file package to get the file
  228.     specification.
  229.     
  230. ------------------------*/
  231.  
  232. void Journal::DoFileButton()
  233. {
  234.     Point        where ;
  235.     Rect        *temp ;
  236.     int            i ;
  237.     long        dirID, procID ;
  238.     short        volNum ;
  239.     SFReply        jFile;
  240.     OSErr        err ;
  241.  
  242.     P_JrnlState    theJrnlState ;
  243.  
  244.     /* center the SF dialog around the cdev dialog window */
  245.     
  246.       temp = &(dp->port.portRect) ;
  247.     
  248.     where.h = (*temp).left + ((*temp).right - (*temp).left) ;    
  249.     where.v = (*temp).top + ((*temp).bottom - (*temp).top) ;
  250.  
  251.     HLock(journalState) ;
  252.     theJrnlState = (P_JrnlState)(*journalState) ;
  253.  
  254.     if (theJrnlState->hasFile == 0)
  255.         SFPutFile(where, FILE_PROMPT, DEFAULT_FILE_NAME, NULL, &jFile) ;
  256.     else
  257.         SFPutFile(where, FILE_PROMPT, (theJrnlState->fName), NULL, &jFile) ;
  258.  
  259.     /* if the user give a valid file, set the start/stop button
  260.        state to active (they can now record). Also, update the journal
  261.        state. */
  262.  
  263.     if (jFile.good) {
  264.         DoHiliteControl(lastItem + START_STOP_BUTTON, CONTROL_ACTIVE) ;
  265.         theJrnlState->hasFile = 1 ;
  266.         for (i = 0 ; i <= jFile.fName[0] ; i++)
  267.             theJrnlState->fName[i] = jFile.fName[i] ;
  268.         GetWDInfo(jFile.vRefNum, &volNum, &dirID, &procID) ;
  269.         theJrnlState->vRefNum = volNum ;
  270.         theJrnlState->dirID = dirID ;
  271.     }
  272.     
  273.     HUnlock(journalState) ;
  274.     ChangedResource(journalState) ;
  275.     WriteResource(journalState) ;
  276. }
  277.  
  278.  
  279.  
  280. /*----------------------
  281.  
  282.     DoSetFileCreator
  283.  
  284.     The user pressed the file button to select a
  285.     journal file.
  286.     
  287.     Use the standard file package to get the file
  288.     specification.
  289.     
  290. ------------------------*/
  291.  
  292. void Journal::DoSetFileCreator()
  293. {
  294.     Point        where ;
  295.     Rect        *temp ;
  296.     int            i ;
  297.     SFReply        jFile;
  298.     OSErr        err ;
  299.     SFTypeList    theTypes ;
  300.     FInfo        fileInfo ;
  301.     char        *osPtr, creatStr[5] ;
  302.     Handle        itemHandle ;
  303.     int            aType ;
  304.     Rect        aRect ;
  305.  
  306.     P_JrnlState    theJrnlState ;
  307.  
  308.     /* center the SF dialog around the cdev dialog window */
  309.     
  310.       temp = &(dp->port.portRect) ;
  311.     
  312.     where.h = (*temp).left + ((*temp).right - (*temp).left) ;    
  313.     where.v = (*temp).top + ((*temp).bottom - (*temp).top) ;
  314.  
  315.  
  316.     theTypes[0] = 'APPL' ;
  317.     SFGetFile(where, CREATOR_PROMPT, NULL, 1,  &theTypes, NULL, &jFile) ;
  318.  
  319.     /* if the user give a valid file, set the start/stop button
  320.        state to active (they can now record). Also, update the journal
  321.        state. */
  322.  
  323.     if (jFile.good) {
  324.         HLock(journalState) ;
  325.         theJrnlState = (P_JrnlState)(*journalState) ;
  326.  
  327.         err = GetFInfo(jFile.fName, jFile.vRefNum, &fileInfo) ;
  328.         theJrnlState->creator = fileInfo.fdCreator ;
  329.  
  330.         osPtr = (char*) &(fileInfo.fdCreator) ;
  331.         creatStr[0] = 4 ;
  332.         creatStr[1] = *osPtr++ ; creatStr[2] = *osPtr++ ;
  333.         creatStr[3] = *osPtr++ ; creatStr[4] = *osPtr++ ;
  334.         
  335.         GetDItem(dp, lastItem + FILE_CREATOR_STRING, &aType, &itemHandle, &aRect) ;
  336.         SetIText(itemHandle, creatStr) ;
  337.         
  338.  
  339.         HUnlock(journalState) ;
  340.         ChangedResource(journalState) ;
  341.         WriteResource(journalState) ;
  342.     }
  343.     
  344. }
  345.  
  346.  
  347.  
  348. /*----------------------
  349.  
  350.     DoStartStopButton
  351.     
  352.     The user pressed the start/stop button. Either start
  353.     the journal recording or stop it.
  354.  
  355. ------------------------*/
  356.  
  357. void Journal::DoStartStopButton(void)
  358. {
  359.     ControlHandle    theControl ;
  360.     int                aType, controlValue ;
  361.     Rect            aRect ;
  362.     P_JrnlState    theJrnlState ;
  363.     
  364.     GetDItem(dp, lastItem + START_STOP_BUTTON, &aType, (Handle)&theControl, &aRect) ;
  365.     
  366.     HLock(journalState) ;
  367.     theJrnlState = (P_JrnlState)(*journalState) ;
  368.     
  369.     if (theJrnlState->recording != 0) {
  370.         DoDriverStuff(FALSE) ;
  371.         theJrnlState->recording = 0 ;
  372.         theJrnlState->hasFile = 0 ;
  373.         SetCTitle(theControl, START_STRING) ;
  374.         SetButtonState(TRUE) ;
  375.         HiliteControl(theControl, CONTROL_INACTIVE) ;
  376.     }
  377.     else {
  378.         DoDriverStuff(TRUE) ;
  379.         theJrnlState->recording = 1 ;
  380.         SetCTitle(theControl, STOP_STRING) ;
  381.         SetButtonState(FALSE) ;
  382.     }
  383.     
  384.     HUnlock(journalState) ;
  385.     ChangedResource(journalState) ;
  386.     WriteResource(journalState) ;
  387. }
  388.  
  389.  
  390. /*----------------------
  391.  
  392.     SetButtonState
  393.     
  394.     Enable or disable the check buttons and the
  395.     file pick button based on the parameter.
  396.  
  397. ------------------------*/
  398.  
  399. void Journal::SetButtonState(Boolean activate)
  400. {
  401.     ControlHandle    theControl ;
  402.     int                aType, activateValue ;
  403.     Rect            aRect ;
  404.  
  405.     if (activate)
  406.         activateValue = CONTROL_ACTIVE ;
  407.     else
  408.         activateValue = CONTROL_INACTIVE ;
  409.     
  410.     DoHiliteControl(lastItem + TIME_STAMP_CHECK, activateValue) ;
  411.     DoHiliteControl(lastItem + FILE_BUTTON, activateValue) ;
  412. }
  413.  
  414.  
  415. /*----------------------
  416.  
  417.     DoDriverStuff
  418.     
  419.     Start or stop the driver based on the parameter.
  420.     
  421.     If starting the journal driver, need to tell it
  422.     what file to open. Pass the appropriate
  423.     paramenter in a control call.
  424.  
  425. ------------------------*/
  426.  
  427. void Journal::DoDriverStuff(Boolean isStart)
  428. {
  429.     OSErr            err ;
  430.     P_JrnlState        theJrnlState ;
  431.     int                dRefNum, i ;
  432.     JrnlFileParam    params ;
  433.     
  434.     HLock(journalState) ;
  435.     theJrnlState = (P_JrnlState)(*journalState) ;
  436.  
  437.     if (isStart) {
  438.         err = OpenDriver(JRNL_DRVR_NAME, &dRefNum) ;
  439.         if (err == noErr) {
  440.             theJrnlState->driverRefNum = dRefNum ;
  441.  
  442.             for (i = 0 ; i <= theJrnlState->fName[0] ; i++)
  443.                 params.fName[i] = theJrnlState->fName[i] ;
  444.             params.vRefNum = theJrnlState->vRefNum ;
  445.             params.dirID = theJrnlState->dirID ;
  446.             params.eventFlags = theJrnlState->eventFlags ;
  447.             params.creator = theJrnlState->creator ;
  448.             err = Control(dRefNum, JRNL_OPEN_FILE, ¶ms) ;
  449.             err = Control(dRefNum, JRNL_ON, NULL) ; 
  450.         }
  451.     }
  452.     else
  453.         err = Control((theJrnlState->driverRefNum), JRNL_OFF, NULL) ;
  454.  
  455.     HUnlock(journalState) ;
  456. }
  457.  
  458.  
  459.  
  460.  
  461. /*----------------------
  462.  
  463.     DoHiliteControl
  464.     
  465.     Set the hilite of the specified control item to theHilite.
  466.  
  467. ------------------------*/
  468.  
  469. void Journal::DoHiliteControl(int theItem, Boolean theHilite)
  470. {
  471.     ControlHandle    theControl ;
  472.     int                aType ;
  473.     Rect            aRect ;
  474.  
  475.     GetDItem(dp, theItem, &aType, (Handle)&theControl, &aRect) ;
  476.     HiliteControl(theControl, theHilite) ;
  477. }
  478.  
  479.  
  480. /*----------------------
  481.  
  482.     DoSetControl
  483.     
  484.     Set the control value of the specified control item
  485.     to theValue.
  486.  
  487. ------------------------*/
  488.  
  489. void Journal::DoSetControl(int theItem, int theValue)
  490. {
  491.     ControlHandle    theControl ;
  492.     int                aType ;
  493.     Rect            aRect ;
  494.  
  495.     GetDItem(dp, theItem, &aType, (Handle)&theControl, &aRect) ;
  496.     SetCtlValue(theControl, theValue) ;
  497. }